package com.gohi.main.store.service.impl;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.gohi.core.service.impl.DefaultService;
import com.gohi.main.store.hbm.ShopgooSpecialoffer;
import com.gohi.main.store.hbm.jdbcMapper.SpseciaofferMapper;
import com.gohi.main.store.service.IDiscountService;
import com.gohi.main.util.global.GloblConfig;



public class DiscountServiceImpl extends DefaultService<Object, String> implements
		IDiscountService{
	
	@Override
	public List<ShopgooSpecialoffer> queryDiscount(ShopgooSpecialoffer special) {
	// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				StringBuffer  sql = new StringBuffer("select g.*,s.address as address ,s.telephone as telephone,s.business_starttime as business_starttime,s.business_endtime as business_endtime  from shopgoo_specialoffer as g, shopgoo_store as s  where 1=1 and g.end_time>=unix_timestamp(now())  and  s.is_audit=2 and s.store_state=2 and g.is_on_shelf=1   and g.store_id=s.store_id and g.is_audit=2 ");
				if(special!=null){
				/*	//ID查询
					if(group.getGroup_id()!=null &&  group.getGroup_id()>0){
						sql.append(" and g.group_id="+group.getGroup_id());
					}*/
					
					if(special.getCity_id()!=null && special.getCity_id()>0){
						sql.append(" and g.city_id="+special.getCity_id());
					}
					
					//商铺查询
					if(special.getStore_id()!=null){
						sql.append(" and g.store_id="+special.getStore_id());
					}
					
			/*		//区域查询
					if(special.getClass_id()!=null && special.getClass_id()>0){
						sql.append(" and g.s_class_id="+special.getClass_id());
					}*/
					
					
					/*//商铺查询
					if(group.getStore_id()!=null){
						sql.append(" and g.store_id="+group.getStore_id());
					}
					
					//区域查询
				
					
					
					
					//品牌查询
					if(!Utils.show(group.getBrand_id()).equals("") && group.getBrand_id()!=0){
						sql.append(" and s.brand_id="+group.getBrand_id());
					}
					
					//商圈查询
					if(!Utils.show(group.getArea_business_id()).equals("") && group.getArea_business_id()!=0){
						sql.append(" and s.area_business_id="+group.getArea_business_id());
					}
					
					
					//价格查询
					if(!Utils.show(group.getMinprice()).equals("") ){
						sql.append(" and g.group_price BETWEEN "+group.getMinprice() +" and "+group.getMaxprice());
					}	
					
					
					//名称查询
					if(!Utils.show(group.getGroup_name()).equals("") ){
						sql.append(" and g.group_name like '%"+group.getGroup_name() +"%'");
					}	
					
					//行政区域查询
					if(!Utils.show(group.getMall_id()).equals("") && group.getMall_id()!=0){
						if( !Utils.show(group.getArea_id()).equals("")){
							sql.append(" and s.area_id="+group.getMall_id());
						}else{
							sql.append(" and s.mall_id="+group.getMall_id());
						}
						
					}
					=================================================================================================================
					*/
					
					//System.err.println(group.getLimit());

				}
				
				//结果排序
				sql.append(" order by buyer_num desc ");
				//分页
				sql.append(" limit "+special.getLimit()*GloblConfig.PAGE_SIZE +","+GloblConfig.PAGE_SIZE);
			//   System.err.println(sql.toString());
				JdbcTemplate jdbc = getDAO().getJdbcTemplate();
				return jdbc.query(sql.toString(), new SpseciaofferMapper());
	}

	@Override
	public ShopgooSpecialoffer discountById(String id) {
		// TODO Auto-generated method stub
		String sql = "select g.*,s.address as address,s.telephone as telephone,s.business_starttime as business_starttime,s.business_endtime as business_endtime from shopgoo_specialoffer as g, shopgoo_store as s  where g.store_id =s.store_id  and g.id="+id;
		//System.err.println(sql);
		JdbcTemplate jdbc = getDAO().getJdbcTemplate();
		return (ShopgooSpecialoffer) jdbc.queryForObject(sql, new SpseciaofferMapper());
	}



	

}
